package com.vmladenov.cook.core.db;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import com.google.gson.JsonObject;
import com.vmladenov.cook.core.RestHelper;
import com.vmladenov.cook.core.db.proxy.BigRecipe;
import com.vmladenov.cook.core.db.proxy.RecipeComment;
import com.vmladenov.cook.core.db.proxy.SmallListProxy;
import com.vmladenov.cook.core.html.Recipe;
import com.vmladenov.cook.core.html.SmallPreview;

public class RecipesRepository extends WebServiceRepository implements ISmallPreviewListRepository
{
	private final String servicePath = "RecipeService/";
	
	@Override
	protected String getServicePath()
	{
		return servicePath;
	}	

	public long getRowCount()
	{
		return getRowCount("GetRecipesCount");
	}

	public ArrayList<SmallPreview> getPreviews(long start, int sorting)
	{
		return getPreviews("GetRecipes", start, sorting);
	}

	public ArrayList<SmallPreview> searchRecipes(String query)
	{
		return searchPreviews("SearchRecipes", query);
	}

	public ArrayList<SmallPreview> getLatestRecipes()
	{
		String url = siteUrl + servicePath + "GetLatestRecipes";
		SmallListProxy[] recipes = RestHelper.GetJSON(url, SmallListProxy[].class);
		return convertToSmallPreview(recipes);
	}

	public Recipe getRecipe(long id)
	{
		String url = String.format(siteUrl + servicePath + "GetRecipe?id=%d&userId=%d", id, getUserId());
		BigRecipe recipeProxy = RestHelper.GetJSON(url, BigRecipe.class);
		if (recipeProxy == null) return null;
		return recipeProxy.toRecipe();
	}

	public Recipe getRecipeOfTheDay()
	{
		String url = siteUrl + servicePath + "GetRandomRecipe";
		BigRecipe recipeProxy = RestHelper.GetJSON(url, BigRecipe.class);
		if (recipeProxy == null) return null;
		return recipeProxy.toRecipe();
	}

	public ArrayList<SmallPreview> getFavoriteRecipes()
	{
		String url = String.format(siteUrl + servicePath + "GetFavoriteRecipes?userId=%d", getUserId());
		SmallListProxy[] recipes = RestHelper.GetJSON(url, SmallListProxy[].class);
		return convertToSmallPreview(recipes);
	}

	public void addFavorite(long recipeId)
	{
		String url = siteUrl + servicePath + "SetFavorite";
		JsonObject param = new JsonObject();
		param.addProperty("recipeId", recipeId);
		param.addProperty("userId", getUserId());
		RestHelper.JSONPost(url, param);
	}

	public void removeFavorite(long recipeId)
	{
		String url = siteUrl + servicePath + "UnSetFavorite";
		JsonObject param = new JsonObject();
		param.addProperty("recipeId", recipeId);
		param.addProperty("userId", getUserId());
		RestHelper.JSONPost(url, param);
	}

	public RecipeComment[] getRecipeComments(long recipeId)
	{
		String url = String.format(siteUrl + servicePath + "GetRecipeComments?recipId=%d", recipeId);
		return RestHelper.GetJSON(url, RecipeComment[].class);
	}

	public void addRecipeComment(long recipeId, String comment)
	{
		String url = siteUrl + servicePath + "AddRecipeComment";
		JsonObject param = new JsonObject();
		param.addProperty("recipeId", recipeId);
		param.addProperty("userId", getUserId());
		param.addProperty("comment", comment);
		RestHelper.JSONPost(url, param);
	}

	public void editRecipeComment(long recipeCommentId, String comment)
	{
		String url = siteUrl + servicePath + "EditRecipeComment";
		JsonObject param = new JsonObject();
		param.addProperty("recipeCommentId", recipeCommentId);
		param.addProperty("comment", comment);
		RestHelper.JSONPost(url, param);
	}

	public void removeRecipeComment(long recipeCommentId)
	{
		String url = siteUrl + servicePath + "RemoveRecipeComment";
		JsonObject param = new JsonObject();
		param.addProperty("recipeCommentId", recipeCommentId);
		RestHelper.JSONPost(url, param);
	}

	public void setRecipeRating(long recipeId, double rating)
	{
		String url = siteUrl + servicePath + "SetRecipeRating";
		JsonObject param = new JsonObject();
		param.addProperty("recipeId", recipeId);
		param.addProperty("userId", getUserId());
		param.addProperty("rating", rating);
		RestHelper.JSONPost(url, param);
	}

	public ArrayList<SmallPreview> getRecipesByProducts(ArrayList<String> products)
	{
		String productParam = Join(products, ",");
		String url = siteUrl + servicePath + "GetRecipesByProduct?products=" + productParam;
		SmallListProxy[] recipes = RestHelper.GetJSON(url, SmallListProxy[].class);
		return convertToSmallPreview(recipes);
	}

	private static String Join(Collection<? extends CharSequence> s, String delimiter)
	{
		int capacity = 0;
		int delimLength = delimiter.length();
		Iterator<? extends CharSequence> iter = s.iterator();
		if (iter.hasNext())
		{
			capacity += iter.next().length() + delimLength;
		}

		StringBuilder buffer = new StringBuilder(capacity);
		iter = s.iterator();
		if (iter.hasNext())
		{
			buffer.append(iter.next());
			while (iter.hasNext())
			{
				buffer.append(delimiter);
				buffer.append(iter.next());
			}
		}
		return buffer.toString();
	}

}
